package com.kestrel.space.common.utils;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;


/**
 * @author yebolin
 * @date 2024/9/10
 */
public class DOCXConvertToPDF {
    public static ByteArrayOutputStream convert(InputStream in) throws Exception {
        try {
            // 从输入流创建 XWPFDocument
            XWPFDocument document = new XWPFDocument(in);

            // 创建 PDF 选项
            PdfOptions options = PdfOptions.create();

            // 创建 ByteArrayOutputStream 用于存储转换后的 PDF
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();

            // 进行转换
            PdfConverter.getInstance().convert(document, outStream, options);

            return outStream;
        } catch (Exception e) {
            throw new Exception("转换失败", e);
        }
    }
}
